﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Data;

namespace LH.U8API.Tool
{
    public static class U8APIHelper
    {

        #region 数据格式转换

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="doc"></param>
        /// <param name="parameter"></param>
        public static void ConvertU8HeadXML<T>(MSXML2.IXMLDOMDocument2 doc, T parameter)
        {
            CheckLicense();
             
            MSXML2.IXMLDOMNode node = doc.selectSingleNode("//rs:data");
            MSXML2.IXMLDOMElement element = doc.createElement("z:row");
            ConvertU8Parameter(element, parameter);
            node.appendChild(element);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="doc"></param>
        /// <param name="parameter"></param>
        public static void ConvertU8BodyXML<T>(MSXML2.IXMLDOMDocument2 doc, List<T> parameter)
        {
            CheckLicense();


            MSXML2.IXMLDOMNode node = doc.selectSingleNode("//rs:data");
            foreach (T body in parameter)
            {
                MSXML2.IXMLDOMElement element = doc.createElement("z:row");
                ConvertU8Parameter(element, body);
                node.appendChild(element);
            }
        }

        /// <summary>
        /// 获取U8参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="element"></param>
        /// <param name="parameter"></param>
        public static void ConvertU8Parameter<T>(MSXML2.IXMLDOMElement element, T parameter)
        {
            CheckLicense();

            foreach (PropertyInfo pi in parameter.GetType().GetProperties())
            {
                if (pi.GetValue(parameter, null) != null && !string.IsNullOrEmpty(pi.GetValue(parameter, null).ToString()))
                {
                    element.setAttribute(pi.Name, pi.GetValue(parameter, null));
                }
            }
        }

        public static List<T> ConvertXMLToU8Entity<T>(MSXML2.IXMLDOMDocument2 doc) where T:new()
        {
            CheckLicense();

            MSXML2.IXMLDOMNode node = doc.selectSingleNode("//rs:data");
            if (!node.hasChildNodes())
            {
                return null;
            }
            List<T> list = new List<T>();
            List<PropertyInfo> properties = typeof(T).GetProperties().ToList();
            foreach (MSXML2.IXMLDOMNode childNode in node.childNodes)
            {
                MSXML2.IXMLDOMElement element = childNode as MSXML2.IXMLDOMElement;
                if (element.attributes.length > 0)
                {
                    T entity = new T();
                    foreach (var pi in properties)
                    {
                        string tempName = pi.Name;
                        if (!pi.CanWrite)
                            continue;
                        object value = element.getAttribute(tempName);
                        if (value != null&&value!=DBNull.Value)
                        {
                            pi.SetValue(entity, value, null);
                        }
                    }
                    list.Add(entity);
                }
            }
            return list;
        }

        /// <summary>
        /// 获得集合实体
        /// datatable转实list<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> EntityList<T>(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            List<T> list = new List<T>();
            T entity = default(T);
            foreach (DataRow dr in dt.Rows)
            {
                entity = Activator.CreateInstance<T>();
                PropertyInfo[] pis = entity.GetType().GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (dt.Columns.Contains(pi.Name))
                    {
                        if (!pi.CanWrite)
                        {
                            continue;
                        }
                        if (dr[pi.Name] != DBNull.Value)
                        {
                            Type t = pi.PropertyType;
                            pi.SetValue(entity, dr[pi.Name].ToString(), null);

                        }
                    }
                }
                list.Add(entity);
            }
            return list;
        }


        #endregion


        public static ADODB.Connection GetNewConnection(ADODB.Connection con, String newDBName, String oldDBName)
        {
            try
            {
                ADODB.Connection newCon = new ADODB.Connection();
                newCon.ConnectionString = con.ConnectionString.Replace(oldDBName, newDBName);
                return newCon;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static void CheckLicense()
        {
            //if (System.DateTime.Now > Convert.ToDateTime("2160-9-20"))
            //{
            //    throw new System.Exception("程序试用结束!请购买正式程序...");
            //}
        }

    }
}
